/*
 *所有关于cw_djlc_analyze_total_table类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*cw_djlc_analyze_total_tableService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using System.Collections.Generic;
using VOL.Core.DBManager;
using System;
using VOL.Core.ManageUser;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;

namespace VOL.Finance.Services
{
    public partial class cw_djlc_analyze_total_tableService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Icw_djlc_analyze_total_tableRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public cw_djlc_analyze_total_tableService(
            Icw_djlc_analyze_total_tableRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        readonly WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 修改存在两个地方。
        ///     一个是汇总表中存采购、研发对应的内容。该表只存最新内容
        ///     一个在原因表中记录内容。不分采购跟研发。该表只记新增。不进行更改
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {

            UpdateOnExecuting = (cw_djlc_analyze_total_table cw_djlc_analyze_total_table, object addList, object updateList, List<object> delKeys) =>
            {

                cw_djlc_analyze_total_table cw_djlc_analyze_total_table1 = DBServerProvider.DbContext.Set<cw_djlc_analyze_total_table>().Where(x => x.cw_djlc_analyze_total_id == cw_djlc_analyze_total_table.cw_djlc_analyze_total_id).FirstOrDefault();
                if (cw_djlc_analyze_total_table1.is_rational.IsNullOrEmpty() || cw_djlc_analyze_total_table1.caigou_is_rational.IsNullOrEmpty())
                {
                    cw_djlc_analyze_total_table1.is_rational = cw_djlc_analyze_total_table.is_rational;
                    cw_djlc_analyze_total_table1.reason = cw_djlc_analyze_total_table.reason;
                    cw_djlc_analyze_total_table1.reasonfileurl = cw_djlc_analyze_total_table.reasonfileurl;
                    cw_djlc_analyze_total_table1.caigou_is_rational = cw_djlc_analyze_total_table.caigou_is_rational;
                    cw_djlc_analyze_total_table1.feedback = cw_djlc_analyze_total_table.feedback;
                    cw_djlc_analyze_total_table1.feedbackurl = cw_djlc_analyze_total_table.feedbackurl;
                    cw_djlc_analyze_total_table1.reasonCreator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                    cw_djlc_analyze_total_table1.reasonCreateDate = DateTime.Now;
                    DBServerProvider.DbContext.Update(cw_djlc_analyze_total_table1);
                }
                else
                {
                    cw_djlc_analyze_total_table1.is_rational = cw_djlc_analyze_total_table.is_rational;
                    cw_djlc_analyze_total_table1.reason = cw_djlc_analyze_total_table.reason;
                    cw_djlc_analyze_total_table1.reasonfileurl = cw_djlc_analyze_total_table.reasonfileurl;
                    cw_djlc_analyze_total_table1.caigou_is_rational = cw_djlc_analyze_total_table.caigou_is_rational;
                    cw_djlc_analyze_total_table1.feedback = cw_djlc_analyze_total_table.feedback;
                    cw_djlc_analyze_total_table1.feedbackurl = cw_djlc_analyze_total_table.feedbackurl;
                    cw_djlc_analyze_total_table1.reasonModifier = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                    cw_djlc_analyze_total_table1.reasonModifyDate = DateTime.Now;
                    DBServerProvider.DbContext.Update(cw_djlc_analyze_total_table1);
                }

                using (var db = DBServerProvider.DbContext)
                {
                    cw_djlc_djzb cw_djlc_djzb = DBServerProvider.DbContext.Set<cw_djlc_djzb>().Where(x => x.cw_djlc_djzb_id == cw_djlc_analyze_total_table.cw_djlc_djzb_id).FirstOrDefault();
                    cw_djlc_analyze_reason cw_djlc_analyze_reason1 = new cw_djlc_analyze_reason();
                    cw_djlc_analyze_reason1.cw_djlc_diff_analyze_id = Guid.NewGuid();
                    cw_djlc_analyze_reason1.cw_djlc_djzb_id = cw_djlc_analyze_total_table.cw_djlc_djzb_id;
                    cw_djlc_analyze_reason1.cw_djlc_code = cw_djlc_djzb.cw_djlc_code;
                    cw_djlc_analyze_reason1.groupnum = cw_djlc_analyze_total_table1.groupnum;
                    cw_djlc_analyze_reason1.groupnumname = cw_djlc_analyze_total_table1.groupnumname;
                    cw_djlc_analyze_reason1.is_rational = cw_djlc_analyze_total_table.is_rational;
                    cw_djlc_analyze_reason1.reason = cw_djlc_analyze_total_table.reason;
                    cw_djlc_analyze_reason1.reasonfileurl = cw_djlc_analyze_total_table.reasonfileurl;
                    cw_djlc_analyze_reason1.Creator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                    cw_djlc_analyze_reason1.CreateDate = DateTime.Now;
                    DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Add(cw_djlc_analyze_reason1);
                    DBServerProvider.DbContext.SaveChanges();
                }
                webResponse.Code = "-1";
                
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }
        
        public override PageGridData<cw_djlc_analyze_total_table> GetPageData(PageDataOptions options)
        {
            SummaryExpress = (IQueryable<cw_djlc_analyze_total_table> queryable) =>
            {
                return (queryable as IQueryable<cw_djlc_analyze_total_table>).GroupBy(x => 0).Select(x => new
                {
                    //AvgPrice注意大小写和数据库字段大小写一样
                    oldprice = x.Sum(o => o.oldprice),
                    newprice = x.Sum(o => o.newprice),
                    analyzeprice = x.Sum(o => o.analyzeprice)
                }).FirstOrDefault();

            };
            return base.GetPageData(options);
        }
        /// <summary>
        /// 重新导出方法
        /// </summary>
        /// <param name="pageData"></param>
        /// <returns></returns>
        public override WebResponseContent Export(PageDataOptions pageData)
        {
            ExportColumns = x => new {
                x.cw_djlc_code,
                x.groupnum,
                x.groupnumname,
                x.analyzeprice,
                x.is_rational,
                x.reason,
                x.caigou_is_rational,
                x.feedback,
            };
            return base.Export(pageData);
        }

        /// <summary>
        /// 导入方法
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public override WebResponseContent Import(List<IFormFile> files)
        {
            DownLoadTemplateColumns = x => new {
                x.cw_djlc_code,
                x.groupnum,
                x.groupnumname,
                x.analyzeprice,
                x.is_rational,
                x.reason,
                x.caigou_is_rational,
                x.feedback,
            };
            ImportOnExecuting = (List<cw_djlc_analyze_total_table> cw_djlc_analyze_total_table) =>
            {

                foreach (var item in cw_djlc_analyze_total_table)
                {
                    // 汇总表导入是批量修改原因，所以也是update操作

                    cw_djlc_analyze_total_table cw_djlc_analyze_total_table1 = DBServerProvider.DbContext.Set<cw_djlc_analyze_total_table>().Where(x => x.cw_djlc_code == item.cw_djlc_code && x.groupnum == item.groupnum).FirstOrDefault();
                    if (cw_djlc_analyze_total_table1.is_rational.IsNullOrEmpty() || cw_djlc_analyze_total_table1.caigou_is_rational.IsNullOrEmpty())
                    {
                        cw_djlc_analyze_total_table1.is_rational = item.is_rational;
                        cw_djlc_analyze_total_table1.reason = item.reason;
                        cw_djlc_analyze_total_table1.reasonfileurl = item.reasonfileurl;
                        cw_djlc_analyze_total_table1.caigou_is_rational = item.caigou_is_rational;
                        cw_djlc_analyze_total_table1.feedback = item.feedback;
                        cw_djlc_analyze_total_table1.feedbackurl = item.feedbackurl;
                        cw_djlc_analyze_total_table1.reasonCreator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                        cw_djlc_analyze_total_table1.reasonCreateDate = DateTime.Now;
                    }
                    else
                    {
                        cw_djlc_analyze_total_table1.is_rational = item.is_rational;
                        cw_djlc_analyze_total_table1.reason = item.reason;
                        cw_djlc_analyze_total_table1.reasonfileurl = item.reasonfileurl;
                        cw_djlc_analyze_total_table1.caigou_is_rational = item.caigou_is_rational;
                        cw_djlc_analyze_total_table1.feedback = item.feedback;
                        cw_djlc_analyze_total_table1.feedbackurl = item.feedbackurl;
                        cw_djlc_analyze_total_table1.reasonModifier = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                        cw_djlc_analyze_total_table1.reasonModifyDate = DateTime.Now;
                    }
                    DBServerProvider.DbContext.Update(cw_djlc_analyze_total_table1);

                    /// 只要四个值其中有一个就需要存入
                    if (!item.is_rational.IsNullOrEmpty() || !item.reason.IsNullOrEmpty()
                     || !item.caigou_is_rational.IsNullOrEmpty() || !item.feedback.IsNullOrEmpty())
                    {
                        cw_djlc_djzb cw_djlc_djzb = DBServerProvider.DbContext.Set<cw_djlc_djzb>().Where(x => x.cw_djlc_code == item.cw_djlc_code).FirstOrDefault();
                        cw_djlc_analyze_reason cw_djlc_analyze_reason1 = new cw_djlc_analyze_reason();
                        cw_djlc_analyze_reason1.cw_djlc_diff_analyze_id = Guid.NewGuid();
                        cw_djlc_analyze_reason1.cw_djlc_djzb_id = cw_djlc_djzb.cw_djlc_djzb_id;
                        cw_djlc_analyze_reason1.cw_djlc_code = item.cw_djlc_code;
                        cw_djlc_analyze_reason1.groupnum = item.groupnum;
                        cw_djlc_analyze_reason1.groupnumname = item.groupnumname;
                        cw_djlc_analyze_reason1.is_rational = item.is_rational;
                        cw_djlc_analyze_reason1.reason = item.reason;
                        cw_djlc_analyze_reason1.Creator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                        cw_djlc_analyze_reason1.CreateDate = DateTime.Now;
                        DBServerProvider.DbContext.Set<cw_djlc_analyze_reason>().Add(cw_djlc_analyze_reason1);
                        DBServerProvider.DbContext.SaveChanges();
                    }

                }
                webResponse.Code = "-1";
                return webResponse.OK("修改成功！");
            };
            return base.Import(files);
        }

        public override WebResponseContent DownLoadTemplate()
        {
            DownLoadTemplateColumns = x => new {
                x.cw_djlc_code,
                x.groupnum,
                x.groupnumname,
                x.analyzeprice,
                x.is_rational,
                x.reason,
                x.caigou_is_rational,
                x.feedback,
            };
            return base.DownLoadTemplate();
        }

    }
}
